﻿using System;
using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using Web.My.Models;
using System.Web.Mvc;
using Web.Base.Security;
using Ayatta.Contract.Domain;
using Ayatta.Common.Extensions;
using Ayatta.Contract.Component;

namespace Web.My.Controllers
{
    public class RefundController : MyBaseController
    {
        public RefundController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 退款申请
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ActionResult Apply(string param)
        {
            var identity = User.Identity.Identity();
            var model = new RefundControllerModel.Apply();
            var orderItem = OrderService.GetOrderItem(param, identity.Id, false);
            if (orderItem != null)
            {
                if (!orderItem.RefundId.IsNullOrEmpty() && orderItem.RefundStatus.HasValue)
                {
                    return Redirect("/refund/detail/" + orderItem.RefundId);
                }
                var order = OrderService.GetOrder(orderItem.OrderId, identity.Id, true);
               
                model.Order = order;
                model.OrderItem = orderItem;
            }
            ViewBag.Sidebar = false;
            return View(model);
        }
        [HttpPost]
        public ActionResult Apply(string param, int reasonId, string desc)
        {
            var identity = User.Identity.Identity();
            var result = new SkResult<string>();
            var orderItem = OrderService.GetOrderItem(param, identity.Id, false);
            if (!orderItem.RefundId.IsNullOrEmpty() && orderItem.RefundStatus.HasValue)
            {
                result.Message = "已申请过！";
                return Json(result);
            }
            var order = OrderService.GetOrder(orderItem.OrderId, identity.Id, true);
            orderItem = order.OrderItems.FirstOrDefault(o => o.Id.Trim() == param);
            if (orderItem != null)
            {
                var refund = new Refund();
                var time = DateTime.Now;
                var refundId = GenerateId('C', time, identity.Id, order.SellerId);
                refund.Id = refundId;
                refund.Category = "";
                refund.OrderId = order.Id;
                refund.OrderItemId = param;
                refund.Total = orderItem.Total;
                if (order.Status == OrderStatus.WaitSellerSend)
                {
                    var amount = order.GetRefundAmount(param);
                    refund.Return = amount;
                    refund.Payment = orderItem.Total - amount;
                }

                refund.BuyerId = identity.Id;
                refund.BuyerNick = identity.NickName;
                refund.SellerId = orderItem.SellerId;
                refund.SellerNick = order.SellerNick;
                refund.SkuId = orderItem.SkuId;
                refund.ItemId = orderItem.ItemId;
                refund.ItemTitle = orderItem.Title;
                refund.ItemPicture = orderItem.PictureUrl;
                refund.Quantity = orderItem.Quantity;
                refund.Price = orderItem.Price;
                refund.ReasonId = reasonId;
                refund.Reason = "";
                refund.Desc = desc;
                refund.Attachments = "";
                refund.GoodsReturn = false;
                refund.GoodsReturnTime = null;
                refund.GoodsStatus = string.Empty;
                refund.ShippingId = string.Empty;
                refund.RefundAddress = string.Empty;
                refund.ShippingCompany = string.Empty;
                refund.ExpressNo = string.Empty;
                refund.Timeout = time.AddDays(10);
                refund.Extra = string.Empty;
                if (order.Status == OrderStatus.WaitSellerSend)
                {
                    refund.Status = RefundStatus.WaitSellerAgree;
                }
                refund.Version = orderItem.Version;
                refund.ModifiedOn = time;
                refund.CreatedBy = User.Identity.Name;
                refund.CreatedOn = time;
                var status = OrderService.CreateRefund(refund);
                if (status)
                {
                    result.Status = true;
                    result.Data = refund.Id;
                }
                else
                {
                    result.Message = "申请退款失败！";
                }
            }
            else
            {
                result.Message = "参数错误！";
            }
            return Json(result);
        }

        public ActionResult Detail(string param, int action=0)
        {
            var identity = User.Identity.Identity();
            var model = new RefundControllerModel.Detail();

            var refund = OrderService.GetRefund(param, identity.Id);
            if (refund != null)
            {
                var orderItem = OrderService.GetOrderItem(refund.OrderItemId, identity.Id, false, true);
                model.OrderItem = orderItem;
            }
            model.Refund = refund;
            ViewBag.Sidebar = false;
            return View(model);
        }
    }
}
